package com.shen.accountbook.module.base;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.shen.accountbook.R;
import com.shen.accountbook.utils.CustomToastUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.app.Activity.RESULT_OK;
import static android.content.Context.ACTIVITY_SERVICE;

/**
 * Created by Reims
 * init:获得数据new对象等
 * initData:初始化数据
 * initEvent:设置监听事件
 */
public abstract class BaseFragment extends Fragment {
    protected View mRootView;
    Unbinder unbinder;
    protected Context mContext;
    protected FragmentActivity mActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(getResId(), container, false);
        unbinder = ButterKnife.bind(this, mRootView);
        mContext = getActivity();
        mActivity = getActivity();

        return mRootView;
    }

    public abstract int getResId();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        initData();
        initEvent();
    }

    protected void init() {
    }

    protected abstract void initData();

    protected abstract void initEvent();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    protected void showToast(String message){
        CustomToastUtils.showToast(mContext, R.layout.custom_toast,
                message, Gravity.CENTER,3);
    }



    /**
     * 当前在前台的是不是本Activity
     * @return
     */
    protected boolean isCurrentActivity(){
        ActivityManager am = (ActivityManager) mContext.getSystemService(ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        // LogUtils.i("current:" + "包名:"+cn.getPackageName());
        // LogUtils.i("currentclass:" + "类名:"+cn.getClassName());
        if(cn.getPackageName().equals(mContext.getPackageName())) {
            if (cn.getClassName().equals(getClass().getName())) {                   /* 主界面 */
                return true;
            }
        }
        return false;
    }

    /**
     * 覆写startActivity方法，加入切换动画
     */
    public void startActivity(Bundle bundle, Class<?> target) {
        Intent intent = new Intent(mContext, target);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 带回调的跳转
     *
     * @param bundle
     * @param requestCode
     * @param target
     */
    public void startForResult(Bundle bundle, int requestCode, Class<?> target) {
        Intent intent = new Intent(mContext, target);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 不带切换动画
     */
    public void finishSimple() {
        mActivity.finish();
    }

    public void finishResult(Intent intent) {
        mActivity.setResult(RESULT_OK, intent);
        mActivity.finish();
    }

    public void finishResult() {
        mActivity.setResult(RESULT_OK);
        mActivity.finish();
    }
}
